package com.cloudpivot.project.rabbitmq.service;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 通过接口 创建交换机与队列
 */

public class RabbitMQSetup {
    private static final String HOST_NAME = "172.17.100.71";
    private static final Integer HOST_PORT=5672;
    private static final String USER_NAME = "admin";
    private static final String PASSWORD="Bio-San@17";
    private static final String MAY_CUR_VIRTUAL_HOST="/maycur_business";

    private static final String NORMAL_EXCHANGE = "normal_exchange";
    private static final String DLX_EXCHANGE = "dlx_exchange";
    private static final String NORMAL_QUEUE = "normal_queue";
    private static final String RETRY_QUEUE_5M = "retry_queue_5min";
    private static final String RETRY_QUEUE_20M = "retry_queue_20min";
    private static final String RETRY_QUEUE_60M = "retry_queue_60min";
    private static final String ERROR_QUEUE = "error_queue";
    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(HOST_NAME);
        factory.setPort(HOST_PORT);
        factory.setUsername(USER_NAME);
        factory.setPassword(PASSWORD);
        factory.setVirtualHost(MAY_CUR_VIRTUAL_HOST);
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            //声明正常交换机
            channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT, true, false, new HashMap<>());

            // 声明死信交换机
            channel.exchangeDeclare(DLX_EXCHANGE, BuiltinExchangeType.DIRECT, true, false, new HashMap<>());

            // 声明死信队列
            Map<String, Object> retryQueue1Args = new HashMap<>();
            retryQueue1Args.put("x-dead-letter-exchange", "normal_exchange");
            retryQueue1Args.put("x-dead-letter-routing-key", "normal");
            retryQueue1Args.put("x-message-ttl",300000); // 5min
            channel.queueDeclare(RETRY_QUEUE_5M, true, false, false, retryQueue1Args);

            Map<String, Object> retryQueue2Args = new HashMap<>();
            retryQueue2Args.put("x-dead-letter-exchange", "normal_exchange");
            retryQueue2Args.put("x-dead-letter-routing-key", "normal");
            retryQueue2Args.put("x-message-ttl", 1200000); // 20min
            channel.queueDeclare(RETRY_QUEUE_20M, true, false, false, retryQueue2Args);

            Map<String, Object> retryQueue3Args = new HashMap<>();
            retryQueue3Args.put("x-dead-letter-exchange", "normal_exchange");
            retryQueue3Args.put("x-dead-letter-routing-key", "normal");
            retryQueue3Args.put("x-message-ttl", 3600000); // 20min
            channel.queueDeclare(RETRY_QUEUE_60M, true, false, false, retryQueue3Args);

            channel.queueDeclare(ERROR_QUEUE, true, false, false, new HashMap<>());

            // 绑定死信队列到死信交换机
            channel.queueBind(RETRY_QUEUE_5M, DLX_EXCHANGE, "retry_5m");
            channel.queueBind(RETRY_QUEUE_20M, DLX_EXCHANGE, "retry_20m");
            channel.queueBind(RETRY_QUEUE_60M, DLX_EXCHANGE, "retry_60m");
            channel.queueBind(ERROR_QUEUE, DLX_EXCHANGE, "error");

            // 声明正常队列，并配置死信交换机
            Map<String, Object> normalQueueArgs = new HashMap<>();
            normalQueueArgs.put("x-dead-letter-exchange", DLX_EXCHANGE);
            normalQueueArgs.put("x-dead-letter-routing-key", "retry_5m"); // 初始重试路由键
            channel.queueDeclare(NORMAL_QUEUE, true, false, false, normalQueueArgs);

            //绑定正常队列到正常交换机
            channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "normal");

            System.out.println("Queues and exchanges declared.");
        }catch (Exception e){
            e.printStackTrace();

        }
    }
}
